<?php
namespace Admin\Controller;

use Common\Controller\AdminbaseController;

class EventController extends AdminbaseController{

    public function index(){
      
        if(!empty($_GET['event_name']) || !empty($_POST['event_name']))      $where['e.event_name']      = $_GET['event_name'] ? I('get.event_name'):I('post.event_name');
        if(!empty($_GET['customer_mobile']) || !empty($_POST['customer_mobile'])) $where['c.mobile'] = $_GET['customer_mobile']? I('get.customer_mobile'):I('post.customer_mobile'); 
        
        $this->assign('event_name',$where['e.event_name']);
        $this->assign('customer_mobile',$where['c.customer_mobile']);
        
        if(!empty($_GET['event_id'])) $where['e.id'] = $_GET['event_id'];
       
        $Event      = M('Event');
        
        $count      = $Event->alias('e')->join('__CUSTOMER__ c on c.id = e.customer_id','left')->where($where)->count();
        
        $page       = $this->page($count, 15);


        $event_list = $Event->alias('e')
                            ->join('__CUSTOMER__ c on c.id = e.customer_id','left')
                            ->where($where)
                            ->order('e.begin_time desc')
                            ->limit($page->firstRow, $page->listRows)
                            ->field('e.id,e.event_name,e.customer_id,e.ball_rule,e.event_type,e.begin_time,e.end_time,e.auth_code,c.customer_name,c.mobile as customer_mobile')
                            ->select();
       
        $this->assign('list',$event_list);
        $this->assign("page", $page->show('Admin'));

    	$this->display();
    }

    //添加赛事
    public function add(){
        $System_config = D('Common/SystemConfig');
        $this->assign('event_type_list',$System_config->getSystemConfigItem('event_type'));
    	$this->display();
    }
    
    //添加赛事
    public function event_post(){
        $customer_mobile               = I('customer_mobile');
        $event_info['event_name']      = I('post.event_name');
        $event_info['customer_id']     = M('Customer')->where('mobile = '.$customer_mobile)->getField('id');
        $event_info['begin_time']      = I('post.begin_time');
        $event_info['end_time']        = I('post.end_time');
        $event_info['auth_code']       = I('post.auth_code');
        $event_info['ball_rule']       = I('post.ball_rule');
        $event_info['event_type']      = I('post.event_type');
        $event_info['event_logo']      = I('post.event_logo');
        //验证授权码
        if(!preg_match('|^\d{8}$|',$event_info['auth_code'])) {
            $this->error("授权码必须是8位纯数字！");
        }
        if(!$result = M('Customer')->where(array('mobile'=>$customer_mobile))->find()){
            $this->error("不存在该手机号的客户！");
        }else if(M('Event')->where('auth_code='.$event_info['auth_code'])->find()){
            $this->error("该授权码已经被使用！");
        }else{
            if($result = M('Event')->add($event_info)){
                addActionLog('event_add',$result);
                $this->success("添加成功！", U("Event/index"));
            }else{
                $this->error("添加失败！");
            }
        }
    }

    //赛事编辑
    public function edit(){
        if(IS_POST){
            $event_info['event_name'] = I('post.event_name');
            $event_info['ball_rule']  = I('post.ball_rule');
            $event_info['begin_time'] = I('post.begin_time');
            $event_info['end_time']   = I('post.end_time');
            $event_info['event_logo'] = I('post.event_logo');
            $event_info['event_type'] = I('post.event_type');
            $where['id'] = I('post.id');
            $result      = M('Event')->where($where)->save($event_info);
            if($result === 0 || $result){
                addActionLog('event_edit',$where['id']);
                $this->success("修改成功！", U("Event/index"));
            }else{
                $this->error("修改失败！");
            }

        }else{
            $id            = (int)$_GET['id'];
            $event_info    = M('Event')->where('id = '.$id)->find();
            $customer_info = M('Customer')->where('id = '.$event_info['customer_id'])->field('mobile,customer_name')->find();
            $event_info['event_logo'] = $event_info['event_logo'] ? $event_info['event_logo'] :"http://bmss.pool-data.com/image/logo.png";
            
            $System_config = D('Common/SystemConfig');
            $this->assign('event_type_list',$System_config->getSystemConfigItem('event_type'));
            
            $this->assign('customer_info',$customer_info);
            $this->assign('event_info',$event_info);
            $this->display();
        }
        
    }

    //验证用户的手机号
    public function checkCustomerMobile(){
    	$Customer        = M('Customer');
    	$customer_mobile = I('get.customer_mobile');
    	if(!$result = $Customer->where(array('mobile'=>$customer_mobile))->find()){
    		$url                = U('Customer/add');
    		$data['status']     = 0;
    		$data['info'] =  '不存在该手机号的客户，是否去<a href="'.$url.'">添加</a>';	
    	}else{
    		$data['status']     = 1;
    		$data['info']       = $result['customer_name'];
    	}
        $this->ajaxReturn($data);
    }

    //自动获取授权码
    public function createAuthCode(){
    	$data['auth_code'] = getAuthCode();
    	$this->ajaxReturn($data);
    }

       //照片上传
    public function upload(){
        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize   =     3145728 ;// 设置附件上传大小
        $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $upload->rootPath  =     './Uploads/'; // 设置附件上传根目录
        $upload->savePath  =     ''; // 设置附件上传（子）目录
        // 上传文件 
        $info   =   $upload->upload();
        $result['img_url']  = 'http://'.$_SERVER['SERVER_NAME'].'/bp_admin/Uploads/'.$info[0]['savepath'].$info[0]['savename'];
        $this->ajaxReturn($result);
    }
}